#!/usr/bin/env python3

import sys
import argparse
import asyncio
from pathlib import Path

# 添加项目根目录到 Python 路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))


async def run_api_server():
    """运行 API 服务器"""
    import uvicorn
    from shared.config import settings
    
    uvicorn.run(
        "api.app:app",
        host=settings.api_host,
        port=settings.api_port,
        reload=False
    )


async def run_mcp_server():
    """运行 MCP 服务器"""
    from mcp.server import main as mcp_main
    await mcp_main()


def run_cli():
    """运行命令行版本"""
    from md2ppt.main import main as cli_main
    cli_main()


def main():
    """主入口点"""
    parser = argparse.ArgumentParser(
        description="MD2PPT - Convert Markdown to PowerPoint",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  python -m md2ppt                    # Run CLI version
  python -m md2ppt --api              # Run API server
  python -m md2ppt --mcp               # Run MCP server
  python -m md2ppt input.md            # Convert file (CLI mode)
        """
    )
    
    parser.add_argument(
        "--api", 
        action="store_true", 
        help="Run as API server"
    )
    
    parser.add_argument(
        "--mcp", 
        action="store_true", 
        help="Run as MCP server"
    )
    
    parser.add_argument(
        "input_file", 
        nargs="?", 
        help="Input Markdown file (for CLI mode)"
    )
    
    parser.add_argument(
        "-o", "--output", 
        help="Output PowerPoint file"
    )
    
    parser.add_argument(
        "--theme", 
        default="default",
        help="Presentation theme"
    )
    
    args = parser.parse_args()
    
    if args.api:
        print("🚀 Starting MD2PPT API Server...")
        asyncio.run(run_api_server())
    elif args.mcp:
        print("🔧 Starting MD2PPT MCP Server...")
        asyncio.run(run_mcp_server())
    else:
        # CLI 模式
        if args.input_file:
            # 设置 CLI 参数
            sys.argv = ["md2ppt", args.input_file]
            if args.output:
                sys.argv.extend(["-o", args.output])
            if args.theme:
                sys.argv.extend(["--theme", args.theme])
        
        run_cli()


if __name__ == "__main__":
    main()